﻿#region Usings

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common;
using ScrumTable.Common.FileSystem;
using ScrumTable.UI.ViewModel;
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
using Image = System.Windows.Controls.Image;
using Point = System.Windows.Point;
using Size = System.Windows.Size;

#endregion

namespace ScrumTable.UI.View.Ink
{
    /// <summary>
    /// InkCanvasUtil
    /// </summary>
    internal static class BitmapEncoderContextFactory 
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string DefaultImageFileExtension = ".jpg";
        private const int DefaultImageQualityLevel = 100; // quality level from 1 to 100

        private static readonly IDictionary<string, Func<BitmapEncoder>> Encoders;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
     
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        static BitmapEncoderContextFactory()
        {
            Encoders = new Dictionary<string, Func<BitmapEncoder>>
                           {
                               { DefaultImageFileExtension, () => new JpegBitmapEncoder() { QualityLevel = DefaultImageQualityLevel } },
                               { ".gif", () => new GifBitmapEncoder() },
                               { ".png", () => new PngBitmapEncoder() }
                           };
        }
    
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        internal static BitmapEncoderContext CreateEncoderContext(string name)
        {
            string extension = GetEncoderExtension(name);

            return new BitmapEncoderContext(
                ToEncoderBasedName(name),
                extension,
                Encoders[extension]());
        }

        private static string GetEncoderExtension(string name)
        {
            string encoderBasedName = ToEncoderBasedName(name);
            string extension = PathUtil.GetExtensionSave(encoderBasedName);

            if (Encoders.ContainsKey(extension))
            {
                return extension;
            }
            return DefaultImageFileExtension;
        }

        private static string ToEncoderBasedName(string name)
        {
            return (name ?? string.Empty).Trim('.');
        }

        #endregion
    }
}
